﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ContactCRUD.Classes
{
    public class Contact : BaseObject,IDataErrorInfo
    {

        public void Reset()
        {
            Name = string.Empty;
            Surname = string.Empty;
            Email = string.Empty;
            Phone = string.Empty;
        }
        public bool IsEmpty()
        {
            if (string.IsNullOrWhiteSpace(_name) && string.IsNullOrWhiteSpace(_surname) && string.IsNullOrWhiteSpace(_phone) && string.IsNullOrWhiteSpace(_email))
            {
                return true;
            }
            return false;
        }
        public Contact()
        {
            ErrorCollection = new System.Collections.Specialized.NameValueCollection();
        }
        #region Public Methods

        private int _companyId;

        public int CompanyId
        {
            get { return _companyId; }
            set { _companyId = value; }
        }
        private string _name;

        public System.Collections.Specialized.NameValueCollection ErrorCollection { get; set; }
        
        public string Name
        {
            get { return _name; } 
            set { _name = value; NotifyChanged("Name");} 
        }

        private string _surname;
        public string Surname
        {
            get { return _surname; }
            set { _surname = value; NotifyChanged("Surname"); }
        }

        private string _phone;
        public string Phone
        {
            get { return _phone; }
            set { _phone = value; NotifyChanged("Phone"); }
        }

        private string _email;
        public string Email
        {
            get { return _email; }
            set { _email = value; NotifyChanged("Email"); }
        }

        #endregion

        private void AddError(string propertyName, string errorMessage)
        {
            if (ErrorCollection[propertyName] == null)
            {
                ErrorCollection.Add(propertyName, errorMessage);
            }
        }
        private void RemoveError(string propertyName)
        {
            if (ErrorCollection[propertyName] != null)
            {
                ErrorCollection.Remove(propertyName);
            }
        }

        #region IDataErrorInfo
        public string Error
        {
            get { return string.Empty; }
        }
        public string this[string columnName]
        {
            get
            {
                string message = string.Empty;
                
                switch (columnName)
                {
                    case "Name":
                        if (!string.IsNullOrWhiteSpace(_name) && _name.Length < 10)
                        {
                            message = "Name must be minimum 10 characters long";   
                        }
                    break;
                    case "Surname":
                    if (!string.IsNullOrWhiteSpace(_surname) && _surname.Length < 10)
                        {
                            message = "Surrname must be minimum 10 characters long";
                        }
                    break;
                    case "Phone":
                    if (!string.IsNullOrWhiteSpace(_phone) && _phone.Length < 10)
                    {
                        message = "Phone Must be minimum 10 characters";
                    }
                    break;
                    case "Email":
                    if (!string.IsNullOrWhiteSpace(_email) && _email.Length < 10)
                    {
                        message = "Email Must be minimum 10 characters";
                    }
                    break;
                }
                if (message == string.Empty)
                {
                    RemoveError(columnName);
                }
                else
                {
                    AddError(columnName, message);
                }

                return message;
            }
        }

        #endregion
    }
}
